#include "glew.h"
#include "wglew.h"
#include "SmoothForm.h"
#include "GLGeometry.h"

static float rotAngle = 0.;

SmoothForm::SmoothForm(HINSTANCE hin)
: GLForm(hin)
{
 
}

SmoothForm::~SmoothForm(void)
{

}

void SmoothForm::OnCreate()
{
    GLfloat values[2];
    glGetFloatv(GL_LINE_WIDTH_GRANULARITY, values);
    printf("GL_LINE_WIDTH_GRANULARITY value is %3.1f\n", values[0]);

    glGetFloatv(GL_LINE_WIDTH_RANGE, values);
    printf("GL_LINE_WIDTH_RANGE values are %3.1f %3.1f\n", values[0], values[1]);

    glEnable(GL_LINE_SMOOTH);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
    glLineWidth(1.5);

    glClearColor(0.0, 0.0, 0.0, 0.0);
}

void SmoothForm::OnSize(int w, int h)
{
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if (w <= h) 
        gluOrtho2D (-1.0, 1.0, 
        -1.0*(GLfloat)h/(GLfloat)w, 1.0*(GLfloat)h/(GLfloat)w);
    else 
        gluOrtho2D (-1.0*(GLfloat)w/(GLfloat)h, 
        1.0*(GLfloat)w/(GLfloat)h, -1.0, 1.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void SmoothForm::OnDraw()
{
    glClear(GL_COLOR_BUFFER_BIT);

    glColor3f (0.0, 1.0, 0.0);
    glPushMatrix();
    glRotatef(-rotAngle, 0.0, 0.0, 0.1);
    glBegin (GL_LINES);
    glVertex2f (-0.5, 0.5);
    glVertex2f (0.5, -0.5);
    glEnd ();
    glBegin(GL_POLYGON);
    glVertex2f(0, 0);
    glVertex2f(0.5, 0.5);
    glVertex2f(0, 0.5);
    glEnd();
    glPopMatrix();

    glColor3f (0.0, 0.0, 1.0);
    glPushMatrix();
    glRotatef(rotAngle, 0.0, 0.0, 0.1);
    glBegin (GL_LINES);
    glVertex2f(0.5, 0.5);
    glVertex2f(-0.5, -0.5);
    glEnd();
    glPopMatrix();

   

    glFlush();
}

void SmoothForm::OnKeyDown(GLKeyEventArgsPtr key_args)
{
   if (key_args->m_vk_code == 'F') {
       rotAngle += 10;
   } else if (key_args->m_vk_code == 'G') {
       rotAngle -= 10;
   }

   if (rotAngle >= 360.) rotAngle = 0.;
   if (rotAngle < -360) rotAngle = 0;
   Repaint();
}

void SmoothForm::OnMouseDown(GLMouseEventArgsPtr args)
{
  
}

void SmoothForm::OnMouseMove(GLMouseEventArgsPtr args)
{
   
}

void SmoothForm::OnMouseUp(GLMouseEventArgsPtr args)
{
}
